package cn.vshare.controller;

import cn.vshare.entity.Log;
import cn.vshare.service.ExcelExportService;
import cn.vshare.service.LogService;
import cn.vshare.util.DateUtil;
import cn.vshare.util.ExcelView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LogController {

    @Autowired
    private LogService logService;
    @Autowired
    private ExcelExportService excelExportService;

    /**
     * @param currPage 当前页面
     * @param pageSize 每页显示条数
     * @param request
     * @return
     */
    @RequestMapping("/getLogs")
    public Map<String, Object> getLogs(int currPage, int pageSize, HttpServletRequest request) {
        Map<String, Object> logMap = new HashMap<String, Object>();
        Page<Log> logPage = logService.getAllLogs(currPage, pageSize); //获取日志信息
        logMap.put("currPage", currPage);
        logMap.put("pageSize", pageSize);
        logMap.put("totalPage", logPage.getPages());
        logMap.put("totalRows", logPage.getTotal());
        logMap.put("logList", logPage);
        return logMap;
    }

    /**
     * 删除日志
     *
     * @param log
     * @return
     */
    @RequestMapping("/delLog")
    public String delLog(Log log) {
        logService.delLog(log);
        return "success";
    }

    /**
     * 批量删除日志
     */

    @RequestMapping("/bactchDelLog")
    public String bactchDelLog(String ids) {
        List<String> idList = JSON.parseArray(ids, String.class); //将字符串转换成集合
        Log log = new Log();
        for (String s : idList) {
            log.setId(s);
            logService.delLog(log);
        }
        return String.valueOf(idList.size()); //返回删除条数
    }

    /**
     * 查询日志
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param currPage  当前页面
     * @param pageSize  页面显示条数
     * @param message   日志内容
     * @return
     */
    @RequestMapping("/queryLogs")
    public Map<String, Object> queryLogs(String startTime, String endTime, int currPage, int pageSize, String message) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Log> logPage = logService.queryLogs(startTime, endTime, currPage, pageSize, message);
        map.put("totalPage", logPage.getPages());
        map.put("currPage", currPage);
        map.put("totalRows", logPage.getTotal());
        map.put("logList", logPage);
        return map;
    }

    /**
     * 将日志导出excel表
     *
     * @param logs
     * @return
     */
    @RequestMapping("/exportExcel")
    public ModelAndView exportExcel(String logs) {
        ModelAndView mv = new ModelAndView();
        //定义excel配置
        ExcelView excelView = new ExcelView(excelExportService);
        //设置文件名
        String fileName = "系统操作日志" + DateUtil.getCurrDate() + ".xls";
        excelView.setFileName(fileName);
        List<Log> logList = JSON.parseArray(logs, Log.class);
        mv.addObject(logList); //添加文件
        mv.setView(excelView);
        return mv;
    }


    @RequestMapping("/exportAll")
    public ModelAndView exportAll() {
        ModelAndView mv = new ModelAndView();
        //定义excel配置
        ExcelView excelView = new ExcelView(excelExportService);
        //设置文件名
        String fileName = "系统所有操作日志" + DateUtil.getCurrDate() + ".xls";
        excelView.setFileName(fileName);
        List<Log> logList = logService.exportAllLogs(); //获取所有日志
        mv.addObject(logList);//添加文件
        mv.setView(excelView);
        return mv;
    }

    @RequestMapping("/countLoginTimes")
    public Map<String, Object> countLoginTimes(String countInfo, String condition) {
        JSONArray jsonArray = JSON.parseArray(countInfo); //将字符串转换成json数组
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < jsonArray.size(); i++) { //将json数组中字符串转换成json对象
            JSONObject jsonObject = JSON.parseObject(jsonArray.get(i).toString());
            jsonObject.put("message", condition);
            int loginTimes = logService.countLoginTimesByTime(jsonObject); //获取登录次数
            map.put(jsonObject.get("timeRange").toString(), loginTimes); //存储统计信息
        }
        return map;
    }


    /**
     * 跳转系统日志页面
     *
     * @return
     */
    @RequestMapping("/forwardSystemLog")
    public ModelAndView forwardSystemLog() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("h-ui/system-log");
        return mv;
    }

    public LogService getLogService() {
        return logService;
    }

    public void setLogService(LogService logService) {
        this.logService = logService;
    }

    public ExcelExportService getExcelExportService() {
        return excelExportService;
    }

    public void setExcelExportService(ExcelExportService excelExportService) {
        this.excelExportService = excelExportService;
    }
}
